package com.saceem.imagenes.test;

import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageTester {

	@SuppressWarnings("deprecation")
	public static void main(String arg[]) {
		JFrame fr;
		File f = null;
		BufferedImage bi = null;
		ImageReadParam readParam = null;
		ImageReader rdr = null;
		Iterator<ImageReader> itr = null;
		String defaultFilename = new String(
				"C:\\Users\\Public\\Pictures\\Sample Pictures\\06020014126.tif");

		if (arg.length > 0)
			f = new File(arg[0]);
		else
			f = new File(defaultFilename);
		try {
			itr = ImageIO.getImageReaders(ImageIO.createImageInputStream(f));
			if (!itr.hasNext()) {
				return;
			}

			rdr = (ImageReader) itr.next();
			rdr.setInput(ImageIO.createImageInputStream(f));
			readParam = rdr.getDefaultReadParam();
			// readParam.getSourceRenderSize().setSize(500, 500);
			readParam.setSourceRegion(new Rectangle(0, 0, 700, 300));
			if (true) {
				
				bi = rdr.read(rdr.getMinIndex(), readParam);
			} else {
				bi = rdr.read(rdr.getMinIndex());
			}

		} catch (IOException excp) {
			System.err.println(excp);
			return;
		}

		fr = new JFrame("JAI Image");
		fr.setSize(800, 500);
		fr.setResizable(true);
		// fr.getContentPane().createImage(300, 300);
		//ImageIcon im = new ImageIcon(bi);
		// im.getIconHeight();
		// im.getIconWidth();

		ScrollPane sp = new ScrollPane(0);
		sp.add(new JLabel(new ImageIcon(bi)));
		fr.getContentPane().add(sp);
		// fr.getContentPane().add(new JLabel(new ImageIcon(bi)));
		fr.pack();
		fr.show();
	}

}